import { create, update, remove, query } from '../../../framework/service/index';

export default {

  namespace: 'partTime',

  state: {
    jobList: [],
    adList:[],
    pageNum: 1,
    pageSize:10,
    pages: 0,
    CityArr: [],
    index: 0,
    cityName:'',
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/part-time') {
          dispatch({type: 'query' })
          // dispatch({type: 'getCityList'})
          dispatch({type: 'getAd'})
          // dispatch({
          //   type: 'relations/querySuccess',
          //   payload:{
          //     limitPost:false
          //   }
          // })

          dispatch({
            type: 'allPartTime/querySuccess',
            payload:{
              isShow:-1,
              ordeyIndex:0,
              areaIndex:0,
              typeNameIndex:0,
              key:'',
              typeId:'',
              district :'',
              jobList:[]
            }
          })
        }
      })
    },
  },

  effects: {

    *query({ payload }, { call, put, select }) {

      const { pageSize } = yield select (({partTime}) => partTime);
      const { cityName } = yield select (({relations}) => relations);
      const pageNum = payload && payload.pageNum ? payload.pageNum : 1;

      const city = cityName.slice(0,cityName.length-1);
      // console.log('city',city);
      const { code, data, message } = yield call(query, '/api/pub/jobs', {
        pageNum,
        pageSize,
        city
      });

      // console.log('code', JSON.stringify(code));

      if (code === 200) {

        const currentList = yield select(({ partTime }) => partTime.jobList);
        let list = data.records;
        if (pageNum > 1) {
          list = currentList.concat(data.records);
        }
        let newList = [];

        list.map((item, index) => {
          if(item.status == 'APPLYING'){
            newList.push(item);
          }
        })

        yield put({
          type: 'querySuccess',
          payload: {
            jobList: newList,
            pageNum: data.current,
            pageSize: data.size,
            pages: data.pages,
          }
        })
      }else{
        console.log('获取兼职列表失败');
      }
    },

    // 获取企业城市列表；
    *getCityList({ payload }, { call, put,select }) {
      // const profile = yield select (({partTime}) => partTime.profile);

      const { code, data, message } = yield call(query, '/api/pub/job/pcd', {});
      // console.log('getCityList',data);
      if (code === 200 && data) {
        // console.log('城市列表',data);
        yield put({
          type: 'querySuccess',
          payload: {
            CityArr: data,
          }
        })
      }
    },

    //获取广告列表
    *getAd({ payload }, { call, put }) {

      const groupsData = yield call(query, '/api/pub/ad/groups', {});

      if(groupsData.code == 200){
        // console.log('所有分组广告数据', JSON.stringify(groupsData.data));
        const groupList = groupsData.data;
        let banner = '';
        groupList.map((item, index) => {
          if(item.identifier == 'banner'){
            banner = item.identifier;
          }
        })

        const { code, data, message } = yield call(query, `/api/pub/ad/records/${banner}`, {});
        // console.log('广告code', JSON.stringify(code));
        // console.log('广告data', JSON.stringify(data));
        // console.log('广告message', JSON.stringify(message));
        if ( code == 200 ) {
          // console.log('广告', JSON.stringify(data.ads));
          yield put({
            type:'querySuccess',
            payload:{
              adList:data.ads,
            }
          })
        }else{
          console.log('获取广告列表首脑', message);
        }

      }else{
        console.log('获取分组广告数据失败', groupsData.message);
      }
    },

  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
